package com.jiwei.system.controller;


import com.jiwei.system.dao.ItemMapper;
import com.jiwei.system.dao.MemberLoginMapper;
import com.jiwei.system.pojo.Book;
import com.jiwei.system.pojo.Borrow;
import com.jiwei.system.pojo.MemberLogin;
import com.jiwei.system.pojo.Things;
import com.jiwei.system.serviceImpl.ItemServiceImpl;
import com.jiwei.system.serviceImpl.MemberServiceImpl;
import com.jiwei.system.utils.CookieUtil;
import com.jiwei.system.utils.JWTUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = {"member", "operator", "admin"},method = RequestMethod.POST)
@Validated
public class ItemController {

    @Resource(name = "ItemService")
    private ItemServiceImpl itemService;

    @Resource
    private MemberLoginMapper memberLoginMapper;



    @PostMapping("getWaitingReturnList")
    public Map<String,Object> getMemberSelfBorrowingItem(HttpServletRequest request){
        MemberLogin memberLogin = (MemberLogin) request.getSession().getAttribute("login");
        String account = memberLogin.getAccount();
        List<Borrow> borrowItemList = itemService.getMemberSelfBorrowingItemList(account);
        Map<String, Object> res= new HashMap<>();
        res.put("code", 0);
        res.put("count", borrowItemList.size());
        res.put("data", borrowItemList);
        res.put("msg", "");
        return res;
    }

    @PostMapping("getThingList")
    public Map<String,Object> getThingList(@RequestParam(value = "page")int page, @RequestParam(value = "limit") int limit){
        List<Things> thingsList = itemService.getThingList(page,limit);
        int count = itemService.getThingListNum();
        Map<String, Object> res= new HashMap<>();
        res.put("code", 0);
        res.put("count", count);
        res.put("data", thingsList);
        res.put("msg", "");
        return res;
    }

    @PostMapping("getBookList")
    public Map<String,Object> getBookList(@RequestParam(value = "page")int page, @RequestParam(value = "limit") int limit){
        List<Book> bookList = itemService.getBookList(page,limit);
        int count = itemService.getBookListNum();
        Map<String, Object> res= new HashMap<>();
        res.put("code", 0);
        res.put("count", count);
        res.put("data", bookList);
        res.put("msg", "");
        return res;
    }

    @PostMapping("borrowThing")
    public String borrowThing(@RequestBody @Validated Borrow borrowThing, HttpServletRequest request){
        MemberLogin memberLogin = (MemberLogin) request.getSession().getAttribute("login");
        String account = memberLogin.getAccount();
        String name = memberLoginMapper.getNameByAccount(account);
        borrowThing.setMemberAccount(account);
        borrowThing.setMemberName(name);
        System.out.println(name);
        itemService.borrowThing(borrowThing);
        return "{" +
                "\"statusCode\":\"" + 200 + '\"' +
                ", \"message\":\"" + "借用物品成功" + "\""+
                '}';
    }

    @PostMapping("returnThing")
    public String returnThing(@RequestBody Borrow borrowThing,HttpServletRequest request){
        MemberLogin memberLogin = (MemberLogin) request.getSession().getAttribute("login");
        String account = memberLogin.getAccount();
        borrowThing.setMemberAccount(account);
        itemService.returnThing(borrowThing);
        return "{" +
                "\"statusCode\":\"" + 200 + '\"' +
                ", \"message\":\"" + "归还物品成功" + "\""+
                '}';
    }


    @PostMapping("borrowBook")
    public String borrowBook(@RequestBody @Validated Borrow borrowBook, HttpServletRequest request){
        MemberLogin memberLogin = (MemberLogin) request.getSession().getAttribute("login");
        String account = memberLogin.getAccount();
        String name = memberLoginMapper.getNameByAccount(account);
        System.out.println(borrowBook);
        borrowBook.setMemberAccount(account);
        borrowBook.setMemberName(name);
        System.out.println(name);
        itemService.borrowBook(borrowBook);
        return "{" +
                "\"statusCode\":\"" + 200 + '\"' +
                ", \"message\":\"" + "借用图书成功" + "\""+
                '}';
    }

    @PostMapping("returnBook")
    public String returnBook(@RequestBody Borrow borrowBook,HttpServletRequest request){
        MemberLogin memberLogin = (MemberLogin) request.getSession().getAttribute("login");
        String account = memberLogin.getAccount();
        borrowBook.setMemberAccount(account);
        System.out.println(borrowBook);
        itemService.returnBook(borrowBook);
        return "{" +
                "\"statusCode\":\"" + 200 + '\"' +
                ", \"message\":\"" + "归还图书成功" + "\""+
                '}';
    }

    @PostMapping("getBorrowHistoryList")
    public Map<String,Object> getBorrowHistoryList(@RequestParam(value = "page")int page,@RequestParam(value = "limit") int limit){
        List<Borrow> ItemList = itemService.getBorrowHistoryList(page, limit);
        int count = itemService.getBorrowHistoryListNum();
        Map<String, Object> res= new HashMap<>();
        res.put("code", 0);
        res.put("count", count);
        res.put("data", ItemList);
        res.put("msg", "");
        return res;
    }

    @PostMapping("addThing")
    public String addThing(@RequestBody @Validated Things things){
//        System.out.println(things);
        int currentAmount = things.getTotalAmount();
        things.setCurrentAmount(currentAmount);
        itemService.addNewThing(things);
        return "{" +
                "\"statusCode\":\"" + 200 + '\"' +
                ", \"message\":\"" + "添加物品成功" + "\""+
                '}';
    }

    @PostMapping("addBook")
    public String addBook(@RequestBody @Validated Book book){
        int currentAmount = book.getTotalAmount();
        book.setCurrentAmount(currentAmount);
        System.out.println(book);
        itemService.addNewBook(book);
        return "{" +
                "\"statusCode\":\"" + 200 + '\"' +
                ", \"message\":\"" + "添加图书成功" + "\""+
                '}';
    }

    @PostMapping("modifyThing")
    public String modifyThing(@RequestBody @Validated Things things){
//        System.out.println(things);
        itemService.modifyThing(things);
        return "{" +
                "\"statusCode\":\"" + 200 + '\"' +
                ", \"message\":\"" + "修改物品成功" + "\""+
                '}';
    }

    @PostMapping("modifyBook")
    public String modifyBook(@RequestBody @Validated Book book){
//        System.out.println(book);
        itemService.modifyBook(book);
        return "{" +
                "\"statusCode\":\"" + 200 + '\"' +
                ", \"message\":\"" + "修改图书成功" + "\""+
                '}';
    }

}
